/*
 * Copyright (C) 2020 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
#ifndef CHPP_GNSS_TYPES_H_
#define CHPP_GNSS_TYPES_H_

// This file was automatically generated by chre_api_to_chpp.py
// Date: 2022-02-03 23:05:31 UTC
// Source: chre_api/include/chre_api/chre/gnss.h @ commit b5a92e457

// DO NOT modify this file directly, as those changes will be lost the next
// time the script is executed

#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>

#include "chpp/app.h"
#include "chpp/common/common_types.h"
#include "chpp/macros.h"
#include "chre_api/chre/gnss.h"
#include "chre_api/chre/version.h"

#ifdef __cplusplus
extern "C" {
#endif

CHPP_PACKED_START

//! See {@link chreGnssClock} for details
struct ChppGnssClock {
  int64_t time_ns;
  int64_t full_bias_ns;
  float bias_ns;
  float drift_nsps;
  float bias_uncertainty_ns;
  float drift_uncertainty_nsps;
  uint32_t hw_clock_discontinuity_count;
  uint16_t flags;
  uint8_t reserved[2];  // Input ignored; always set to 0
} CHPP_PACKED_ATTR;

//! See {@link chreGnssMeasurement} for details
struct ChppGnssMeasurement {
  int64_t time_offset_ns;
  int64_t accumulated_delta_range_um;
  int64_t received_sv_time_in_ns;
  int64_t received_sv_time_uncertainty_in_ns;
  float pseudorange_rate_mps;
  float pseudorange_rate_uncertainty_mps;
  float accumulated_delta_range_uncertainty_m;
  float c_n0_dbhz;
  float snr_db;
  uint16_t state;
  uint16_t accumulated_delta_range_state;
  int16_t svid;
  uint8_t constellation;
  uint8_t multipath_indicator;
  float carrier_frequency_hz;
} CHPP_PACKED_ATTR;

//! See {@link chreGnssDataEvent} for details
struct ChppGnssDataEvent {
  uint8_t version;  // Input ignored; always set to CHRE_GNSS_DATA_EVENT_VERSION
  uint8_t measurement_count;
  uint8_t reserved[6];  // Input ignored; always set to 0
  struct ChppGnssClock clock;
  struct ChppOffset measurements;  // References measurement_count instances of
                                   // struct ChppOffset
} CHPP_PACKED_ATTR;

//! See {@link chreGnssLocationEvent} for details
struct ChppGnssLocationEvent {
  uint64_t timestamp;
  int32_t latitude_deg_e7;
  int32_t longitude_deg_e7;
  float altitude;
  float speed;
  float bearing;
  float accuracy;
  uint16_t flags;
  uint8_t reserved[2];  // Input ignored; always set to 0
  float altitude_accuracy;
  float speed_accuracy;
  float bearing_accuracy;
} CHPP_PACKED_ATTR;

//! CHPP app header plus struct ChppGnssDataEventWithHeader
struct ChppGnssDataEventWithHeader {
  struct ChppAppHeader header;
  struct ChppGnssDataEvent payload;
} CHPP_PACKED_ATTR;

//! CHPP app header plus struct ChppGnssLocationEventWithHeader
struct ChppGnssLocationEventWithHeader {
  struct ChppAppHeader header;
  struct ChppGnssLocationEvent payload;
} CHPP_PACKED_ATTR;

CHPP_PACKED_END

// Encoding functions (CHRE --> CHPP)

/**
 * Converts from given CHRE structure to serialized CHPP type.
 *
 * @param in Fully-formed CHRE structure.
 * @param out Upon success, will point to a buffer allocated with chppMalloc().
 * It is the responsibility of the caller to set the values of the CHPP app
 * layer header, and to free the buffer when it is no longer needed via
 * chppFree() or CHPP_FREE_AND_NULLIFY().
 * @param outSize Upon success, will be set to the size of the output buffer, in
 * bytes.
 *
 * @return true on success, false if memory allocation failed.
 */
bool chppGnssDataEventFromChre(const struct chreGnssDataEvent *in,
                               struct ChppGnssDataEventWithHeader **out,
                               size_t *outSize);

/**
 * Converts from given CHRE structure to serialized CHPP type.
 *
 * @param in Fully-formed CHRE structure.
 * @param out Upon success, will point to a buffer allocated with chppMalloc().
 * It is the responsibility of the caller to set the values of the CHPP app
 * layer header, and to free the buffer when it is no longer needed via
 * chppFree() or CHPP_FREE_AND_NULLIFY().
 * @param outSize Upon success, will be set to the size of the output buffer, in
 * bytes.
 *
 * @return true on success, false if memory allocation failed.
 */
bool chppGnssLocationEventFromChre(const struct chreGnssLocationEvent *in,
                                   struct ChppGnssLocationEventWithHeader **out,
                                   size_t *outSize);

// Decoding functions (CHPP --> CHRE)

/**
 * Converts from serialized CHPP structure to a CHRE type.
 *
 * @param in Fully-formed CHPP structure.
 * @param in Size of the CHPP structure in bytes.
 *
 * @return If successful, a pointer to a CHRE structure allocated with
 * chppMalloc(). If unsuccessful, null. It is the responsibility of the caller
 * to free the buffer when it is no longer needed via chppFree() or
 * CHPP_FREE_AND_NULLIFY().
 */
struct chreGnssDataEvent *chppGnssDataEventToChre(
    const struct ChppGnssDataEvent *in, size_t inSize);

/**
 * Converts from serialized CHPP structure to a CHRE type.
 *
 * @param in Fully-formed CHPP structure.
 * @param in Size of the CHPP structure in bytes.
 *
 * @return If successful, a pointer to a CHRE structure allocated with
 * chppMalloc(). If unsuccessful, null. It is the responsibility of the caller
 * to free the buffer when it is no longer needed via chppFree() or
 * CHPP_FREE_AND_NULLIFY().
 */
struct chreGnssLocationEvent *chppGnssLocationEventToChre(
    const struct ChppGnssLocationEvent *in, size_t inSize);

#ifdef __cplusplus
}
#endif

#endif  // CHPP_GNSS_TYPES_H_
